package com.cuanbo.dualscreenteachfu.adapter;

import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.os.Environment;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.cuanbo.dualscreenteachfu.R;
import com.cuanbo.dualscreenteachfu.activity.MainActivity;
import com.cuanbo.dualscreenteachfu.activity.PreviewActivity;
import com.cuanbo.dualscreenteachfu.bean.ImgBean;
import com.cuanbo.dualscreenteachfu.utils.ActivityUtil;
import com.cuanbo.dualscreenteachfu.utils.BroadcastUtil;
import com.cuanbo.dualscreenteachfu.utils.LogUtil;

import java.io.File;
import java.util.ArrayList;

public class ImgAdapter extends BaseQuickAdapter<ImgBean, BaseViewHolder> implements BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.OnItemLongClickListener {
    private int type;
    public ImgAdapter() {
        super(R.layout.adapter_img_item, new ArrayList<ImgBean>());
        setOnItemClickListener(this);
        setOnItemLongClickListener(this);
    }

    @Override
    protected void convert(BaseViewHolder helper, ImgBean item) {
        ImageView ivImg=helper.getView(R.id.ivImg);
        if(type==1){
            try{
                ivImg.setImageBitmap(getVideoFrame(item.getFileName()));
            }catch (Exception e){}
        }else{
            Glide.with(ivImg).load(item.getFileName()).into(ivImg);
        }
        helper.setText(R.id.tvName,""+item.getSimpleName());
    }

    public void refurshData(String date,int type){
        this.type=type;
        getData().removeAll(getData());
        String strType="/capture/";
        if(type==1){
            strType="/video/";
        }
        File f=new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).getAbsolutePath() + strType+ date+"/");
        if(f.exists()&&f.isDirectory()){
            String[] strs=f.list();
            for (int i=0;i<strs.length;i++){
                getData().add(new ImgBean(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).getAbsolutePath() + strType+ date+"/"+strs[i],strs[i]));
            }
        }
        notifyDataSetChanged();
    }

    public Bitmap getVideoFrame(String videoPath) {
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        try {
            retriever.setDataSource(videoPath);
            Bitmap bitmap = retriever.getFrameAtTime(1000000, MediaMetadataRetriever.OPTION_CLOSEST_SYNC); // 获取1秒处的帧
            retriever.release();
            return bitmap;
        } catch (IllegalArgumentException ex) {
            // 处理异常，例如打印日志或返回null
            ex.printStackTrace();
            return null;
        }
    }


    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
/*        if(MainActivity.isCompare){
            EventBusBean eventBusBean=new EventBusBean(EventBusBean.COMPARE);
            eventBusBean.setData(getItem(position).getFileName());
            EventBus.getDefault().post(eventBusBean);
        }else{
            ImgBean bean=getItem(position);
            ActivityUtil.open(mContext, PreviewActivity.class,"imgPath",bean.getFileName());
        }*/
        ImgBean bean=getItem(position);
        if(type==1){
            BroadcastUtil.sendBroadcastToZhuScreen(12,bean.getFileName());
        }else{
            if(MainActivity.imgType==0){
                ActivityUtil.open(mContext, PreviewActivity.class,"imgPath",bean.getFileName());
            }else if(MainActivity.imgType==1){
                BroadcastUtil.sendBroadcastToZhuScreen(8,bean.getFileName());
            }else if(MainActivity.imgType==2){
                BroadcastUtil.sendBroadcastToZhuScreen(9,bean.getFileName());
            }
        }

        //ActivityUtil.open(mContext, PreviewActivity.class,"imgPath",bean.getFileName());
    }

    @Override
    public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
        try{
            new File(getItem(position).getFileName()).delete();
            getData().remove(position);
            notifyDataSetChanged();
        }catch (Exception e){
            LogUtil.addError(e);
        }
        return true;
    }
}